import java.util.*;

public class RandomNumberGenerator {
    private static final Random RANDOM = new Random();

    public static void main(String[] args) {

        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("London", 1);
        hashMap.put("New York", 1);
        hashMap.put("Dallas", 1);
        hashMap.put("Paris", 1);

        System.out.println(hashMap.keySet());

        List<Integer> numberList = random(30, 0, 10);
        System.out.println("30 Random number from 0~10: " + numberList.toString());

        Map<Integer, Integer> frequencyMap = frequency(numberList);
        System.out.println("Frequency map: " + frequencyMap.toString());

        Set<Integer> maxCountNumbers = mostFrequency(frequencyMap);
        System.out.println("Most frequency numbers: " + maxCountNumbers.toString());

    }

    private static List<Integer> random(int count, int lowerBound, int upperBound) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int number = RANDOM.nextInt(upperBound - lowerBound) + lowerBound;
            list.add(number);
        }
        return list;
    }

    public static Map<Integer, Integer> frequency(List<Integer> list) {
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer number : list) {
            int count = map.getOrDefault(number, 0);
            map.put(number, count + 1);
        }
        return map;
    }

    public static Set<Integer> mostFrequency(Map<Integer, Integer> frequencyMap) {
        int maxCount = -1;
        for (Integer number : frequencyMap.keySet()) {
            if (frequencyMap.get(number) > maxCount) {
                maxCount = frequencyMap.get(number);
            }
        }

        Set<Integer> set = new HashSet<>();
        for (Integer number : frequencyMap.keySet()) {
            if (frequencyMap.get(number) == maxCount) {
                set.add(number);
            }
        }
        return set;
    }
}
